package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionMapper;
import com.tanhua.server.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 根据陌生人id查询mysql问题对象
     *
     * @param userId
     * @return
     */
    public Question queryQuestion(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }

    /**
     * 保存问题
     *
     * @param id      用户id
     * @param content 问题
     * @return
     */
    public Boolean saveQuestion(Long id, String content) {
        //先查有没有该问题
        Question question1 = this.queryQuestion(id);
        if (null != question1) {
            question1.setTxt(content);
            return this.questionMapper.updateById(question1) > 0;
        }else {
            //当查找的问题为空时
            Question question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            return this.questionMapper.insert(question) > 0;
        }
    }
}
